﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
//using StockMonitor.Helper;
using Tigera.T_Definition;
using Tigera.LibCommon;
using Tigera.Network;

namespace StockMonitor
{
    
    public partial class FormLogin : Form
    {
        public FormLogin()
        {
            InitializeComponent();

            Control.CheckForIllegalCrossThreadCalls = false;
            XmlNodeList settingsitems = AppMgr.Cfg.GetNodeList("configuration", "networks");
            

            List<AddressItem> addresss = new List<AddressItem>();
            foreach (XmlNode item in settingsitems)
            {
                AddressItem data = new AddressItem();
                data.Name =item.SelectSingleNode("name").InnerText;
                data.Addr = item.SelectSingleNode("mqserver").InnerText;
                data.Port = int.Parse ( item.SelectSingleNode("mqport").InnerText );  
                data.DispalyName = data.Name + "-" + data.Addr;
                addresss.Add(data);
               
            }
            this.comboBox1.DataSource  = addresss;
            this.comboBox1.DisplayMember = "DispalyName";
            this.comboBox1.ValueMember = "Addr";
            
            this.comboBox1.SelectedIndex = 0;
        }
        public static DialogResult Login()
        {
            if (AuthUserService.Instance.Logined)
            {
                return DialogResult.OK;
            }

            FormLogin dialog = new FormLogin();
            DialogResult ret = dialog.ShowDialog();
           
            return ret;

        }

        
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {            
            this.btnLogin.Enabled = false;
            if (this.tbAccount.Text.Trim() == string.Empty || this.tbPwd.Text.Trim() == string.Empty)
            {
                MessageBox.Show("请输入用户名和密码", "输入用户名密码", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.btnLogin.Enabled = true;
                return;
            }

            AppMgr.Instance.Mqclient.OnLoginResponse -= Mqclient_OnLoginResponse;
            AppMgr.Instance.Mqclient.OnLoginResponse += new EventHandler<TiEventArgs<bool>>(Mqclient_OnLoginResponse);
            AppMgr.Instance.Mqclient.OnConInfo -= Mqclient_OnConInfo;
            AppMgr.Instance.Mqclient.OnConInfo += new EventHandler<TiEventArgs<T_MQConnectionInfoResponse>>(Mqclient_OnConInfo);


            if (!AppMgr.Instance.Mqclient.IsAlive)
            {
                string addr = this.comboBox1.SelectedValue as string;// AppMgr.GetSetting("configuration", "network", "mqserver");
                int port = int.Parse(AppMgr.GetSetting("configuration", "network", "mqport"));
                bool ret = AppMgr.Instance.Mqclient.Open(addr, port);
            }
            else
            {
                T_MQConnectionInfoRequest reqc = new T_MQConnectionInfoRequest();
                AppMgr.Instance.Mqclient.SendDataTo(SpecialExchanges.GetSpecialExchangeName(BusinessType.Monitor), ESHelper.Pack(ref reqc));
            }



            

            LogonRequest req = new LogonRequest();
            req.UserName = this.tbAccount.Text;
            req.PassWord = this.tbPwd.Text;
            
           
            
            AppMgr.Instance.Mqclient.Session.DeclareExchange(AppMgr.TradeRiskExName);

            AppMgr.Instance.Mqclient.SendDataTo(AppMgr.TradeRiskExName, ESHelper.Pack(ref req));
            
            AppMgr.Instance.Mqclient.AddPublicListenMailBox(BusinessType.BulletinTopic, BusinessType.TBulletinTopic.ServerStatusTopic);
        }

        void Mqclient_OnConInfo(object sender, TiEventArgs<T_MQConnectionInfoResponse> e)
        {
           // if (e.Data.TradeRimsServerStatus != AppMgr.Instance.Mqclient.traderimsstatus)
            //{
               // traderimsstatus = info.TradeRimsServerStatus;
            if (e.Data.TradeRimsServerStatus == ComputerStatus.OffLine)
            {
                MessageBox.Show(this, "目前服务器状态" + e.Data.TradeRimsServerStatus.ToString(), "提示", MessageBoxButtons.OK);
                this.btnLogin.Enabled = true;
                //AppMgr.Instance.Mqclient.Close();
            }
            //}
        }

        void Mqclient_OnLoginResponse(object sender, TiEventArgs<bool> e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => DoMqclient_OnLoginResponse(e)));
            }
            else
            {
                DoMqclient_OnLoginResponse(e);
            }            
        }
        void DoMqclient_OnLoginResponse(TiEventArgs<bool> e)
        {
            this.btnLogin.Enabled = true;
            if (e.Data == true)
            {
                AuthUserService.Instance.Logined = true;
                this.DialogResult = DialogResult.OK;

                this.Close();
                AuthUserService.Instance.QueryUserInfo();
            }
            else
            {
                MessageBox.Show(this, "用户密码错误");
                return;
            }
        }
    }


    public class AddressItem
    {
        public string Name
        {
            get;
            set;
        }
        public string DispalyName
        {
            get;
            set;
        }
        public string Addr
        {
            get;
            set;
        }
        public int Port
        {
            get;
            set;
        }
        //public object Self
        //{
        //    get
        //    {
        //        return this;
        //    }
        //    set
        //    {
        //        t
        //        //this = (AddressItem)value;
        //    }
        //}

    }
}
